package com.fr.dialog.core;

import com.fr.base.ColumnRow;
import com.fr.base.Inter;
import com.fr.base.core.BaseCoreUtils;
import com.fr.cell.core.GUICoreUtils;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JFormattedTextField.AbstractFormatter;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JSpinner.DateEditor;
import javax.swing.JSpinner.DefaultEditor;
import javax.swing.JSpinner.NumberEditor;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;
import javax.swing.text.DocumentFilter.FilterBypass;

public class ColumnRowPane extends JPanel
{
  private UpperCaseSpinner columnSpinner;
  private JSpinner rowSpinner;
  private boolean isShowLabel;
  public static String PropChangeName = "ColumnRowPaneChangeName";
  private PropertyChangeSupport changeSupport;
  DocumentListener documentListener;

  public ColumnRowPane()
  {
    this(true);
  }

  public ColumnRowPane(boolean paramBoolean)
  {
    this.isShowLabel = true;
    this.documentListener = new DocumentListener(this)
    {
      private final ColumnRowPane this$0;

      public void changedUpdate()
      {
        this.this$0.newColumnRow();
      }

      public void insertUpdate()
      {
        this.this$0.newColumnRow();
      }

      public void removeUpdate()
      {
        this.this$0.newColumnRow();
      }
    };
    this.isShowLabel = paramBoolean;
    initComponents();
  }

  public void addListener(ChangeListener paramChangeListener)
  {
    this.columnSpinner.addChangeListener(paramChangeListener);
    this.rowSpinner.addChangeListener(paramChangeListener);
  }

  public void initComponents()
  {
    setLayout(new GridLayout(1, 2));
    ArrayList localArrayList1 = new ArrayList();
    for (int i = 1; i < 1000; ++i)
      localArrayList1.add(BaseCoreUtils.convertIntToABC(i));
    ArrayList localArrayList2 = new ArrayList();
    for (int j = 1; j < 5000; ++j)
      localArrayList2.add("" + j);
    SpinnerListModel localSpinnerListModel = new SpinnerListModel(localArrayList1);
    JPanel localJPanel1 = new JPanel();
    add(localJPanel1);
    localJPanel1.setLayout(new FlowLayout(0, 1, 0));
    JLabel localJLabel1 = new JLabel(Inter.getLocText("Column") + ":");
    if (this.isShowLabel)
      localJPanel1.add(localJLabel1);
    this.columnSpinner = new UpperCaseSpinner(this, localSpinnerListModel);
    this.columnSpinner.setPreferredSize(new Dimension(60, 24));
    this.columnSpinner.addChangeListener(new ChangeListener(this)
    {
      private final ColumnRowPane this$0;

      public void stateChanged()
      {
        this.this$0.firePropertyChange(ColumnRowPane.PropChangeName, null, ColumnRowPane.access$000(this.this$0).getValue());
      }
    });
    JFormattedTextField localJFormattedTextField1 = GUICoreUtils.getSpinnerTextField(this.columnSpinner);
    if (localJFormattedTextField1 != null)
    {
      localJFormattedTextField1.setColumns(3);
      localJFormattedTextField1.setHorizontalAlignment(2);
    }
    ((AbstractDocument)localJFormattedTextField1.getDocument()).setDocumentFilter(new DocumentFilter(this)
    {
      private final ColumnRowPane this$0;

      public void insertString(, int paramInt, String paramString, AttributeSet paramAttributeSet)
        throws BadLocationException
      {
        paramFilterBypass.insertString(paramInt, paramString.toUpperCase(), paramAttributeSet);
      }

      public void replace(, int paramInt1, int paramInt2, String paramString, AttributeSet paramAttributeSet)
        throws BadLocationException
      {
        if (paramString != null)
          paramString = paramString.toUpperCase();
        paramFilterBypass.replace(paramInt1, paramInt2, paramString, paramAttributeSet);
      }
    });
    localJPanel1.add(this.columnSpinner);
    JPanel localJPanel2 = new JPanel();
    add(localJPanel2);
    localJPanel2.setLayout(new FlowLayout(0, 1, 0));
    JLabel localJLabel2 = new JLabel(Inter.getLocText("Row") + ":");
    if (this.isShowLabel)
      localJPanel2.add(localJLabel2);
    this.rowSpinner = new JSpinner(new SpinnerListModel(localArrayList2));
    this.rowSpinner.setPreferredSize(new Dimension(60, 24));
    this.rowSpinner.addChangeListener(new ChangeListener(this)
    {
      private final ColumnRowPane this$0;

      public void stateChanged()
      {
        this.this$0.firePropertyChange(ColumnRowPane.PropChangeName, null, ColumnRowPane.access$100(this.this$0).getValue());
      }
    });
    JFormattedTextField localJFormattedTextField2 = GUICoreUtils.getSpinnerTextField(this.rowSpinner);
    if (localJFormattedTextField2 != null)
    {
      localJFormattedTextField2.setColumns(4);
      localJFormattedTextField2.setHorizontalAlignment(2);
    }
    localJPanel2.add(this.rowSpinner);
    addDocumentListener(this.documentListener);
  }

  public void addDocumentListener(DocumentListener paramDocumentListener)
  {
    ((JSpinner.DefaultEditor)this.columnSpinner.getEditor()).getTextField().getDocument().addDocumentListener(paramDocumentListener);
    ((JSpinner.DefaultEditor)this.rowSpinner.getEditor()).getTextField().getDocument().addDocumentListener(paramDocumentListener);
  }

  public void newColumnRow()
  {
    String str1 = ((JSpinner.DefaultEditor)this.columnSpinner.getEditor()).getTextField().getText();
    String str2 = ((JSpinner.DefaultEditor)this.rowSpinner.getEditor()).getTextField().getText();
    if ((str1.length() > 0) && (str2.length() > 0))
    {
      ColumnRow localColumnRow = ColumnRow.valueOf(BaseCoreUtils.convertABCToInt(str1) - 1, Integer.valueOf(str2).intValue() - 1);
      try
      {
        populate(localColumnRow);
      }
      catch (Exception localException)
      {
      }
    }
  }

  public void populate(ColumnRow paramColumnRow)
  {
    int i = paramColumnRow.getColumn();
    this.columnSpinner.setValue(BaseCoreUtils.convertIntToABC(i + 1));
    int j = paramColumnRow.getRow();
    this.rowSpinner.setValue("" + (j + 1));
  }

  public void setEnabled(boolean paramBoolean)
  {
    super.setEnabled(paramBoolean);
    for (int i = 0; i < getComponentCount(); ++i)
    {
      JPanel localJPanel = (JPanel)getComponent(i);
      for (int j = 0; j < localJPanel.getComponentCount(); ++j)
      {
        JComponent localJComponent = (JComponent)localJPanel.getComponent(j);
        localJComponent.setEnabled(paramBoolean);
      }
    }
  }

  public ColumnRow update()
  {
    return ColumnRow.valueOf(BaseCoreUtils.convertABCToInt((String)this.columnSpinner.getValue()) - 1, Integer.parseInt((String)this.rowSpinner.getValue()) - 1);
  }

  protected void firePropertyChange(String paramString, Object paramObject1, Object paramObject2)
  {
    if ((this.changeSupport == null) || ((paramObject1 != null) && (paramObject2 != null) && (paramObject1.equals(paramObject2))))
      return;
    this.changeSupport.firePropertyChange(paramString, paramObject1, paramObject2);
  }

  public synchronized void addPropertyChangeListener(PropertyChangeListener paramPropertyChangeListener)
  {
    if (this.changeSupport == null)
      this.changeSupport = new PropertyChangeSupport(this);
    this.changeSupport.addPropertyChangeListener(paramPropertyChangeListener);
  }

  public synchronized void removePropertyChangeListener(PropertyChangeListener paramPropertyChangeListener)
  {
    if (this.changeSupport == null)
      return;
    this.changeSupport.removePropertyChangeListener(paramPropertyChangeListener);
  }

  public synchronized PropertyChangeListener[] getPropertyChangeListeners()
  {
    if (this.changeSupport == null)
      return new PropertyChangeListener[0];
    return this.changeSupport.getPropertyChangeListeners();
  }

  static UpperCaseSpinner access$000(ColumnRowPane paramColumnRowPane)
  {
    return paramColumnRowPane.columnSpinner;
  }

  static JSpinner access$100(ColumnRowPane paramColumnRowPane)
  {
    return paramColumnRowPane.rowSpinner;
  }

  private class UpperCaseSpinner extends JSpinner
  {
    private final ColumnRowPane this$0;

    public UpperCaseSpinner(, SpinnerModel paramSpinnerModel)
    {
      super(paramSpinnerModel);
      this.this$0 = paramColumnRowPane;
    }

    protected JComponent createEditor()
    {
      if (paramSpinnerModel instanceof SpinnerDateModel)
        return new JSpinner.DateEditor(this);
      if (paramSpinnerModel instanceof SpinnerListModel)
        return new UpperCaseEditor(this, this);
      if (paramSpinnerModel instanceof SpinnerNumberModel)
        return new JSpinner.NumberEditor(this);
      return new JSpinner.DefaultEditor(this);
    }

    private class UpperCaseEditor extends JSpinner.DefaultEditor
    {
      private final ColumnRowPane.UpperCaseSpinner this$1;

      public UpperCaseEditor(, JSpinner paramJSpinner)
      {
        super(paramJSpinner);
        this.this$1 = paramUpperCaseSpinner;
        if (!(paramJSpinner.getModel() instanceof SpinnerListModel))
          throw new IllegalArgumentException("model not a SpinnerListModel");
        getTextField().setEditable(true);
        getTextField().setFormatterFactory(new DefaultFormatterFactory(new ListFormatter(this, null)));
      }

      public SpinnerListModel getModel()
      {
        return ((SpinnerListModel)getSpinner().getModel());
      }

      private class ListFormatter extends JFormattedTextField.AbstractFormatter
      {
        private DocumentFilter filter;
        private final ColumnRowPane.UpperCaseSpinner.UpperCaseEditor this$2;

        private ListFormatter()
        {
          this.this$2 = paramUpperCaseEditor;
        }

        public String valueToString()
          throws ParseException
        {
          if (paramObject == null)
            return "";
          return paramObject.toString();
        }

        public Object stringToValue()
          throws ParseException
        {
          return paramString;
        }

        protected DocumentFilter getDocumentFilter()
        {
          if (this.filter == null)
            this.filter = new Filter(this, null);
          return this.filter;
        }

        ListFormatter(, ColumnRowPane.1 param1)
        {
          this(paramUpperCaseEditor);
        }

        static ColumnRowPane.UpperCaseSpinner.UpperCaseEditor access$400(ListFormatter paramListFormatter)
        {
          return paramListFormatter.this$2;
        }

        static JFormattedTextField access$500(ListFormatter paramListFormatter)
        {
          return paramListFormatter.getFormattedTextField();
        }

        private class Filter extends DocumentFilter
        {
          private final ColumnRowPane.UpperCaseSpinner.UpperCaseEditor.ListFormatter this$3;

          private Filter()
          {
            this.this$3 = paramListFormatter;
          }

          public void replace(, int paramInt1, int paramInt2, String paramString, AttributeSet paramAttributeSet)
            throws BadLocationException
          {
            paramString = paramString.toUpperCase();
            if ((paramString != null) && (paramInt1 + paramInt2 == paramFilterBypass.getDocument().getLength()))
            {
              List localList = ColumnRowPane.UpperCaseSpinner.UpperCaseEditor.ListFormatter.access$400(this.this$3).getModel().getList();
              Object localObject1 = null;
              for (int i = 0; i < localList.size(); ++i)
              {
                Object localObject2 = localList.get(i);
                String str2 = localObject2.toString();
                if ((str2 != null) && (str2.startsWith(paramFilterBypass.getDocument().getText(0, paramInt1) + paramString)))
                {
                  localObject1 = localObject2;
                  break;
                }
              }
              String str1 = (localObject1 != null) ? localObject1.toString() : null;
              if (str1 != null)
              {
                paramFilterBypass.remove(0, paramInt1 + paramInt2);
                paramFilterBypass.insertString(0, str1, null);
                ColumnRowPane.UpperCaseSpinner.UpperCaseEditor.ListFormatter.access$500(this.this$3).select(paramInt1 + paramString.length(), str1.length());
                return;
              }
            }
            super.replace(paramFilterBypass, paramInt1, paramInt2, paramString, paramAttributeSet);
          }

          public void insertString(, int paramInt, String paramString, AttributeSet paramAttributeSet)
            throws BadLocationException
          {
            replace(paramFilterBypass, paramInt, 0, paramString, paramAttributeSet);
          }

          Filter(, ColumnRowPane.1 param1)
          {
            this(paramListFormatter);
          }
        }
      }
    }
  }
}